package 每日一题;

import java.util.HashSet;

public class 快乐数 {
    public boolean isHappy(int n) {
        //如果不是快乐数，会进入循环
        HashSet<Integer> set = new HashSet<>();
        while (n!=1 && !set.contains(n)){
            set.add(n);
            n = nextNum(n);
        }
        return n==1;
    }

    private int nextNum(int n) {
        int nextNum = 0;
        while (n>0){
            nextNum += (n%10)*(n%10);
            n/=10;
        }
        return nextNum;
    }
}
